package com.obao.user.dao.impl;

import com.obao.user.entity.BusinessItem;
import com.obao.user.entity.ProductItem;
import com.obao.user.entity.User;
import org.hibernate.SessionFactory;

import com.obao.user.dao.ICartDao;
import com.obao.user.entity.Cart;

/**
 * @author 代江平
 * @date:2016年11月5日
 */
public class CartDao implements ICartDao {
	private SessionFactory sessionFactory;
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	@Override
	public void save(Cart cart) {
		sessionFactory.getCurrentSession().save(cart);
	}

	@Override
	public void save(BusinessItem businessItem) {
		sessionFactory.getCurrentSession().save(businessItem);
	}

	@Override
	public void save(ProductItem productItem) {
		sessionFactory.getCurrentSession().save(productItem);
	}

	@Override
	public void updateProductItem(Integer number,Integer productItemId) {
		sessionFactory.getCurrentSession().createQuery("update ProductItem set productNumber=productNumber+"+number+"  where productItemId=?")
				.setParameter(0,productItemId)
				.executeUpdate();
	}

	@Override
	public Cart findCart(User user) {
		return (Cart) sessionFactory.getCurrentSession().createQuery("from Cart c where c.user.userId=?")
				.setParameter(0,user.getUserId()).uniqueResult();
	}

	@Override
	public void deleteProductItem(Integer productItemId) {
		sessionFactory.getCurrentSession().createQuery("delete from ProductItem where productItemId=?")
				.setParameter(0,productItemId).executeUpdate();
	}

	@Override
	public void deleteBusinessItem(Integer businessItemId) {
		sessionFactory.getCurrentSession().createQuery("delete from BusinessItem where businessItemId=?")
				.setParameter(0,businessItemId).executeUpdate();
	}

	@Override
	public void deleteCart(Integer cartId) {
		sessionFactory.getCurrentSession().createQuery("delete from Cart where cartId=?").setParameter(0,cartId).executeUpdate();
	}

	@Override
	public ProductItem findProductItem(Integer productItemId) {
		return (ProductItem) sessionFactory.getCurrentSession().get(ProductItem.class,productItemId);
	}

	@Override
	public boolean checkBusinessItemIsExistProductItem(Integer businessItemId) {
		Boolean flag = false;
		long  count = (long) sessionFactory.getCurrentSession().createQuery("select count(*) from ProductItem pt where pt.BusinessItem.businessItemId=?").setParameter(0,businessItemId).uniqueResult();
		if(count>0) {
			flag = true;
		}
		return flag;
	}

	@Override
	public boolean checkCartIsExistBusinessItem(Integer businessItemId) {
		Boolean flag = false;
		BusinessItem businessItem = (BusinessItem) sessionFactory.getCurrentSession().load(BusinessItem.class,businessItemId);
		Integer cartId = businessItem.getCart().getCartId();
		long count = (long) sessionFactory.getCurrentSession().createQuery("select count(*) from BusinessItem b where b.cart.cartId=?").setParameter(0,cartId).uniqueResult();
		if(count>0){
			flag = true;
		}else{
			sessionFactory.getCurrentSession().createQuery("delete from Cart where cartId=?").setParameter(0,cartId).executeUpdate();
		}
		return flag;
	}

	@Override
	public BusinessItem findBusinessItemById(Integer businessItemId) {
		return (BusinessItem) sessionFactory.getCurrentSession().get(BusinessItem.class,businessItemId);
	}


}
